package te;

/**
 * @author tzp
 * @since 2020/12/17
 */
public class LinkTest {
    static class LinkNode {
        int val;
        LinkNode next;

        public LinkNode(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "LinkNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    public static LinkNode reverse(LinkNode head) {
        if (head == null || head.next == null) return head;
        LinkNode dummyHead = null;
        while (head != null) {
            LinkNode tmp = dummyHead;
            dummyHead = head;
            head = head.next;
            dummyHead.next = tmp;//头插法 插入dummyHead链表中
        }
        return dummyHead;
    }

    public static void main(String[] args) {
        LinkNode node1 = new LinkNode(1);
        LinkNode node2 = new LinkNode(2);
        LinkNode node3 = new LinkNode(3);
        node1.next = node2;
        node2.next = node3;
        System.out.println(node1);
    }
}
